package com.boot.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.boot.web.dao.BlindBoxDao;
import com.boot.web.dao.BuyBlindBoxDao;
import com.boot.web.dao.CollageListDao;
import com.boot.web.entity.BuyBlindBox;
import com.boot.web.entity.CollageList;
import com.boot.web.entity.CollageMode;
import com.boot.web.service.CollageModeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.BlindBox;
import com.boot.web.service.BlindBoxService;

/**
 * 盲盒配置控制器
 * @author 【zhouJie】
 * @date 2021-06-04 13:57:46
 */
@RestController
@RequestMapping("/blindBox")
public class BlindBoxController {

    @Autowired
    private BlindBoxService blindBoxService;

    @Autowired
    private BlindBoxDao blindBoxDao;

    @Autowired
    private BuyBlindBoxDao buyBlindBoxDao;

    @Autowired
    private CollageModeService collageModeService;

    @Autowired
    private CollageListDao collageListDao;

    @PostMapping("/save")
    public ResponseServer save(BlindBox blindBox) {
        return ResponseServer.success("添加成功", blindBoxService.save(blindBox));
    }

    @PutMapping("/update")
    public ResponseServer update(BlindBox blindBox) {
        return ResponseServer.success("修改成功", blindBoxService.update(blindBox));
    }

    @PutMapping("/updateConfig")
    public ResponseServer updateConfig(BlindBox blindBox) {
        Integer modeId = blindBox.getCollageModeId();
        if(modeId == null){
            return ResponseServer.error("请选择要生成的盲盒");
        }
        CollageMode cm = collageModeService.getById(modeId);
        int va = cm.getPeopleQuantity();
        if(buyBlindBoxDao.queryBuyBlindBoxByModeId(modeId).size() > 0){
            return ResponseServer.error("当前盲盒已生成，不可重复生成");
        }
        if(collageListDao.queryByModeId(modeId) != null){
            return ResponseServer.error("当前盲盒已生成，不可重复生成");
        }
        CollageList collageList = new CollageList();
        collageList.setCollageModeId(modeId);
        collageList.setStatus("0");
        collageListDao.save(collageList);
        List<BlindBox> boxList = blindBoxDao.queryBlindBoxByModeId(modeId);
        if(boxList.size() > 0){
            for(BlindBox box : boxList){
                int iRandom = 1 + (int)(Math.random()*(va + 1 - 1));
                BuyBlindBox bbb = buyBlindBoxDao.queryBuyBlindBoxByIds(collageList.getId(), iRandom);
                if(bbb == null){
                    BuyBlindBox buy = new BuyBlindBox();
                    buy.setClassifyId(box.getClassifyId());
                    buy.setCollageListId(collageList.getId());
                    buy.setUpdateTime(new Date());
                    buy.setSerialNumber(iRandom);
                    buyBlindBoxDao.save(buy);
                }else{
                    int d;
                    do {
                        d = 1 + (int)(Math.random()*(va + 1 - 1));
                        if (buyBlindBoxDao.queryBuyBlindBoxByIds(collageList.getId(), d) == null) {
                            break;
                        }
                    } while (buyBlindBoxDao.queryBuyBlindBoxByIds(collageList.getId(), d) != null);
                    BuyBlindBox buy = new BuyBlindBox();
                    buy.setClassifyId(box.getClassifyId());
                    buy.setCollageListId(collageList.getId());
                    buy.setUpdateTime(new Date());
                    buy.setSerialNumber(d);
                    buyBlindBoxDao.save(buy);
                }
            }
        }else{
            return ResponseServer.error("当前盲盒还未配置成功，不能生成");
        }
        return ResponseServer.success("生成成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", blindBoxService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", blindBoxService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(blindBoxService.getBlindBoxVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(blindBoxService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(blindBoxService.getById(id));
    }

}
